package demo1;

public class InheritThreadLocalDemo {
    public static void main(String[] args) throws InterruptedException {
        InheritableThreadLocal<String> s1 = new InheritableThreadLocal<>();//父进程的值拷贝给子进程，
        ThreadLocal<String> s2 = new ThreadLocal<>();

        s1.set("main inheritable thread local");
        s2.set("main thread local");
        new Thread(()->{
//            子进程：:main inheritable thread local
            System.out.println(Thread.currentThread().getName()+":"+s1.get());
//            子进程：:null
            System.out.println(Thread.currentThread().getName()+":"+s2.get());
            s1.set("子进程 main inheritable thread local");//不影响父进程的值
            s2.set("子进程 thread local");
//            子进程：:子进程 main inheritable thread local
            System.out.println(Thread.currentThread().getName()+":"+s1.get());
//            子进程：:子进程 thread local
            System.out.println(Thread.currentThread().getName()+":"+s2.get());



        },"子进程：").start();
        Thread.sleep(1000);
//        main:main inheritable thread local
        System.out.println(Thread.currentThread().getName()+":"+s1.get());
//        main:main thread local
        System.out.println(Thread.currentThread().getName()+":"+s2.get());


    }
}
